package com.ruoyi.project.business.domain.rules.daily;

import com.ruoyi.project.business.domain.entity.DailyScheduleEmp;
import com.ruoyi.project.business.domain.entity.Emp;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.Solution;
import com.ruoyi.project.business.domain.model.daily.TimePeriodMirror;
import com.ruoyi.project.business.domain.rules.Rule;
import lombok.Data;

import java.time.LocalTime;
import java.util.ArrayList;

/**
 * 智能排班(日排班) 期望的时间段
 */
@Data
public class ExpectedTime extends Rule {
    private LocalTime expectStart;
    private LocalTime expectEnd;

    @Override
    public JudgeResult judge(Solution solution, Emp emp) {
        DailyScheduleEmp dailyScheduleEmp = (DailyScheduleEmp) emp;
        TimePeriodMirror tryAssignTimePeriod = dailyScheduleEmp.getTryAssignTimePeriod();
        boolean ifCondition = doJudge(tryAssignTimePeriod);
        if(ifCondition) {
            return accept();
        } else {
            JudgeResult res = null;
            if (getBreakIfReject()) {
                res = stopTask();
            } else {
                res = acceptAndReturn();
            }
            res.setTips("emp ExpectedTime 未匹配 " + tryAssignTimePeriod.getSegmentStart().toString() + " ~ " + tryAssignTimePeriod.getSegmentEnd().toString());
            return res;
        }
//        throw new RuleException(this.getClass().getName(), -1);
    }

    private boolean doJudge(TimePeriodMirror tryBingo) {
        LocalTime bingoStartTime = tryBingo.getSegmentStart();
        LocalTime bingoEndTime = tryBingo.getSegmentEnd();

        if((bingoStartTime.isAfter(expectStart) || bingoStartTime.equals(expectStart))
                && (bingoEndTime.isBefore(expectEnd) || bingoEndTime.equals(expectEnd)) ) {
            return true;
        }
        return false;
    }

    @Override
    public void setConflicts() {
        setType(Rule.RULE_TYPE_USER);
        this.conflicts = new ArrayList<>();
    }
}
